#import "record.h"

/*option
*/
bool g_OptionBgm = true;
bool g_OptionSoundex = true;

/*record
*/
bool g_RecordRate = true;
bool g_RecordLibraryTutorial = true;
bool g_RecordExperimentTutorial = true;

/*weapon*/
int g_RecordWeapSelected;
int g_RecordWeapLock[WEAPON_TYPE_COUNT] = {0, 1, 1, 1, 1};

/*item*/
int g_RecordItemSelected[ITEM_SELECT_MAX_COUNT] = {-1, -1};
int g_RecordItemLock[ITEM_TYPE_COUNT] = {1, 1, 1, 1, 1, 1, 1};
int g_RecordItemCount[ITEM_TYPE_COUNT] = {0, 0, 0, 0, 0, 0, 0};
int g_RecordPieceCount[PIECE_TYPE_COUNT] = {3, 3, 3, 3, 0, 0, 0};

/*exhibition*/
int g_RecordExhibitionElementLock[EXHIBITION_ELEMENT_COUNT] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

/*mission*/
int g_RecordMissionLock[MISSION_COUNT + 1] = {0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1};
RecordMission g_RecordMission[MISSION_COUNT + 1];

NSString *filePathInDocument(NSString *fileName)
{
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
	    , NSUserDomainMask
	    , YES);

	NSLog(@"Get document path: %@", [paths objectAtIndex:0]);

	NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:fileName];

	return filePath;
}

void RecordSave(void)
{
	int i;
	char _Path[STRING_BUF_SIZE];
	FILE *_File;
	mxml_node_t *_NodeXml, *_NodeWeapon, *_NodeItem, *_NodeExhibition, *_NodeMission, *_NodeMissionRecord, *_NodeMissionRecordNode;

	memset(_Path, 0, sizeof _Path);
	[filePathInDocument (@"file_record.xml") getCString:_Path maxLength:sizeof _Path encoding:NSASCIIStringEncoding];
	_File = fopen(_Path, "w");
	if (NULL != _File)
	{
		_NodeXml = mxmlNewXML("1.0");
		_NodeWeapon = mxmlNewElement(_NodeXml, "weapon");
		_NodeItem = mxmlNewElement(_NodeXml, "item");
		_NodeExhibition = mxmlNewElement(_NodeXml, "exhibition");
		_NodeMission = mxmlNewElement(_NodeXml, "mission");

		/*option
		*/
		tmxmlNodeAddIntChild(_NodeXml, "state_bgm", g_OptionBgm);
		tmxmlNodeAddIntChild(_NodeXml, "state_effect", g_OptionSoundex);

		tmxmlNodeAddIntChild(_NodeXml, "record_rate", g_RecordRate);
		tmxmlNodeAddIntChild(_NodeXml, "record_library_tutorial", g_RecordLibraryTutorial);
		tmxmlNodeAddIntChild(_NodeXml, "record_experiment_tutorial", g_RecordExperimentTutorial);

		/*weapon
		*/
		tmxmlNodeAddIntChild(_NodeWeapon, "weapon_selected", g_RecordWeapSelected);
		tmxmlNodeAddIntArray(_NodeWeapon, g_RecordWeapLock, WEAPON_TYPE_COUNT, "weapon_lock", "data");

		/*item
		*/
		tmxmlNodeAddIntArray(_NodeItem, g_RecordItemSelected, ITEM_SELECT_MAX_COUNT, "item_selected", "data");
		tmxmlNodeAddIntArray(_NodeItem, g_RecordItemLock, ITEM_TYPE_COUNT, "item_lock", "data");
		tmxmlNodeAddIntArray(_NodeItem, g_RecordItemCount, ITEM_TYPE_COUNT, "item_count", "data");
		tmxmlNodeAddIntArray(_NodeItem, g_RecordPieceCount, PIECE_TYPE_COUNT, "piece_count", "data");

		/*exhibition
		*/
		tmxmlNodeAddIntArray(_NodeExhibition, g_RecordExhibitionElementLock, EXHIBITION_ELEMENT_COUNT, "exhibition_element_lock", "data");

		/*mission
		*/
		tmxmlNodeAddIntArray(_NodeMission, g_RecordMissionLock, MISSION_COUNT + 1, "mission_lock", "data");
		_NodeMissionRecord = mxmlNewElement(_NodeMission, "mission_record");
		for (i = 0; i < MISSION_COUNT + 1; i++)
		{
			_NodeMissionRecordNode = mxmlNewElement(_NodeMissionRecord, "mission_record_node");
			tmxmlNodeAddIntChild(_NodeMissionRecordNode, "flag_star_heart", g_RecordMission[i].flag_star_heart);
			tmxmlNodeAddIntChild(_NodeMissionRecordNode, "flag_star_enemy", g_RecordMission[i].flag_star_enemy);
			tmxmlNodeAddIntChild(_NodeMissionRecordNode, "flag_star_special", g_RecordMission[i].flag_star_special);
		}

		mxmlSaveFile(_NodeXml, _File, tmxmlCallBackXmlWhitespace);
		mxmlDelete(_NodeXml);
		fclose(_File);
	}
	else
	{}
}

void RecordLoad(void)
{
	int _Index;
	char _Path[STRING_BUF_SIZE];
	FILE *_File;
	mxml_node_t *_NodeXml, *_NodeBouy, *_NodeWeapon, *_NodeItem, *_NodeExhibition, *_NodeMission, *_NodeMissionRecord, *_NodeMissionRecordBouy;

	memset(_Path, 0, sizeof _Path);
	[filePathInDocument (@"file_record.xml") getCString:_Path maxLength:sizeof _Path encoding:NSASCIIStringEncoding];
	_File = fopen(_Path, "r");
	if (NULL != _File)
	{
		_NodeXml = mxmlLoadFile(NULL, _File, MXML_TEXT_CALLBACK);
		_NodeBouy = mxmlGetFirstChild(_NodeXml);
		for (; NULL != _NodeBouy; _NodeBouy = mxmlWalkNext(_NodeBouy, _NodeXml, MXML_NO_DESCEND))
		{
			/*option
			*/
			if (tmxmlIsElement(_NodeBouy, "state_bgm"))
			{
				g_OptionBgm = tmxmlElementReadChildInt(_NodeBouy);
			}
			else if (tmxmlIsElement(_NodeBouy, "state_effect"))
			{
				g_OptionSoundex = tmxmlElementReadChildInt(_NodeBouy);
			}
			else if (tmxmlIsElement(_NodeBouy, "record_rate"))
			{
				g_RecordRate = tmxmlElementReadChildInt(_NodeBouy);
			}
			else if (tmxmlIsElement(_NodeBouy, "record_library_tutorial"))
			{
				g_RecordLibraryTutorial = tmxmlElementReadChildInt(_NodeBouy);
			}
			else if (tmxmlIsElement(_NodeBouy, "record_experiment_tutorial"))
			{
				g_RecordExperimentTutorial = tmxmlElementReadChildInt(_NodeBouy);
			}
			/*weapon
			*/
			else if (tmxmlIsElement(_NodeBouy, "weapon"))
			{
				_NodeWeapon = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeWeapon; _NodeWeapon = mxmlWalkNext(_NodeWeapon, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeWeapon, "weapon_selected"))
					{
						g_RecordWeapSelected = tmxmlElementReadChildInt(_NodeWeapon);
					}
					else if (tmxmlIsElement(_NodeWeapon, "weapon_lock"))
					{
						tmxmlReadIntArray(_NodeWeapon, g_RecordWeapLock, "data");
					}
				}
			}
			/*item
			*/
			else if (tmxmlIsElement(_NodeBouy, "item"))
			{
				_NodeItem = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeItem; _NodeItem = mxmlWalkNext(_NodeItem, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeItem, "item_selected"))
					{
						tmxmlReadIntArray(_NodeItem, g_RecordItemSelected, "data");
					}
					else if (tmxmlIsElement(_NodeItem, "item_lock"))
					{
						tmxmlReadIntArray(_NodeItem, g_RecordItemLock, "data");
					}
					else if (tmxmlIsElement(_NodeItem, "item_count"))
					{
						tmxmlReadIntArray(_NodeItem, g_RecordItemCount, "data");
					}
					else if (tmxmlIsElement(_NodeItem, "piece_count"))
					{
						tmxmlReadIntArray(_NodeItem, g_RecordPieceCount, "data");
					}
				}
			}
			/*exhibition
			*/
			else if (tmxmlIsElement(_NodeBouy, "exhibition"))
			{
				_NodeExhibition = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeExhibition; _NodeExhibition = mxmlWalkNext(_NodeExhibition, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeExhibition, "exhibition_element_lock"))
					{
						tmxmlReadIntArray(_NodeExhibition, g_RecordExhibitionElementLock, "data");
					}
				}
			}
			/*mission
			*/
			else if (tmxmlIsElement(_NodeBouy, "mission"))
			{
				_NodeMission = mxmlGetFirstChild(_NodeBouy);
				for (; NULL != _NodeMission; _NodeMission = mxmlWalkNext(_NodeMission, _NodeBouy, MXML_NO_DESCEND))
				{
					if (tmxmlIsElement(_NodeMission, "mission_lock"))
					{
						tmxmlReadIntArray(_NodeMission, g_RecordMissionLock, "data");
					}
					else if (tmxmlIsElement(_NodeMission, "mission_record"))
					{
						_NodeMissionRecord = mxmlGetFirstChild(_NodeMission);
						_Index = 0;
						for (; NULL != _NodeMissionRecord; _NodeMissionRecord = mxmlWalkNext(_NodeMissionRecord, _NodeMission, MXML_NO_DESCEND))
						{
							if (tmxmlIsElement(_NodeMissionRecord, "mission_record_node"))
							{
								_NodeMissionRecordBouy = mxmlGetFirstChild(_NodeMissionRecord);
								for (; NULL != _NodeMissionRecordBouy; _NodeMissionRecordBouy = mxmlWalkNext(_NodeMissionRecordBouy, _NodeMissionRecord, MXML_NO_DESCEND))
								{
									if (tmxmlIsElement(_NodeMissionRecordBouy, "flag_star_heart"))
									{
										g_RecordMission[_Index].flag_star_heart = tmxmlElementReadChildInt(_NodeMissionRecordBouy);
									}
									else if (tmxmlIsElement(_NodeMissionRecordBouy, "flag_star_enemy"))
									{
										g_RecordMission[_Index].flag_star_enemy = tmxmlElementReadChildInt(_NodeMissionRecordBouy);
									}
									else if (tmxmlIsElement(_NodeMissionRecordBouy, "flag_star_special"))
									{
										g_RecordMission[_Index].flag_star_special = tmxmlElementReadChildInt(_NodeMissionRecordBouy);
									}
								}
								_Index++;
							}
						}
					}
				}
			}
		}

		mxmlDelete(_NodeXml);
		fclose(_File);
	}
	else
	{}
}

int RecrodGetTotalStarCount(void)
{
	int i;
	int _Ret;

	_Ret = 0;
	for (i = 1; i < MISSION_COUNT + 1; i++)
	{
		if (g_RecordMission[i].flag_star_heart)
		{
			_Ret++;
		}
		if (g_RecordMission[i].flag_star_enemy)
		{
			_Ret++;
		}
		if (g_RecordMission[i].flag_star_special)
		{
			_Ret++;
		}
	}
	return _Ret++;
}

int RecordGetChapterStarCount(int ChapterIndex)
{
	int _Start, _End;
	int _Ret;

	switch (ChapterIndex)
	{
	case 0: _Start = 1; break;
	case 1: _Start = 6; break;
	case 2: _Start = 11; break;
	default:;
	}
	_End = _Start + 5;
	_Ret = 0;
	for (; _Start < _End; _Start++)
	{
		if (g_RecordMission[_Start].flag_star_heart)
		{
			_Ret++;
		}
		if (g_RecordMission[_Start].flag_star_enemy)
		{
			_Ret++;
		}
		if (g_RecordMission[_Start].flag_star_special)
		{
			_Ret++;
		}
	}
	return _Ret;
}